angular.module('starter.controllers')
.controller('ProductlistCtrl', function($scope, museumFac, $stateParams, $timeout, $location, cfg, $cookies, uploadImg, $ionicScrollDelegate, $rootScope) {
	$scope.width1 = $(window).width();
  	$scope.width2 = ($scope.width1-24)/16*9;

    $scope.id = $stateParams.id;

	$scope.turn = function(flag){
		$scope.header_chosen = flag;
        $scope.getProduct();
	}

	$scope.change_language = function(id){
	    $scope.language = id;
	    var expireDate = new Date();  
		expireDate.setDate(expireDate.getDate() + 30);                
		$cookies.put('language', id, {'expires': expireDate.toUTCString()});
		location.reload();
		if(id == 0){
			document.getElementById('title').innerHTML = '提示';
			document.getElementById('windowclosebutton').value = '确定';
		}else{
			document.getElementById('title').innerHTML = 'Hint';
			document.getElementById('windowclosebutton').value = 'Confirm';
		}

	}

	$scope.getLanguage = function(){
    	$scope.language = $cookies.get('language');
    	if($scope.language){
      		$scope.language = $scope.language;
    	}else{
      		$scope.language = 0;
    	}
    	if($scope.language == 0){
			document.getElementById('title').innerHTML = '提示';
			document.getElementById('windowclosebutton').value = '确定';
		}else{
			document.getElementById('title').innerHTML = 'Hint';
			document.getElementById('windowclosebutton').value = 'Confirm';
		}
        console.log($rootScope.search_product);
  	}
  	$scope.getLanguage();

	$scope.checkOpenid = function(){
        $scope.url = window.location.href;
        wx.ready(function(){
          wx.onMenuShareTimeline({
            title: $scope.share_name + ' | 艺术商店\r\n四方当代美术馆', // 分享标题
            link: $scope.url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: 'http://store.sifangartmuseum.org.cn/Public/museum/www/img/logo.png', // 分享图标
            success: function () { 
                // 用户确认分享后执行的回调函数
            },
            cancel: function () { 
                // 用户取消分享后执行的回调函数
            }
        });
        // 获取“分享给朋友”按钮点击状态及自定义分享内容接口
        wx.onMenuShareAppMessage({
            title: $scope.share_name + ' | 艺术商店',  // 分享标题
            desc: '四方当代美术馆', // 分享描述
            link: $scope.url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: 'http://store.sifangartmuseum.org.cn/Public/museum/www/img/logo.png', // 分享图标
            type: '', // 分享类型,music、video或link，不填默认为link
            dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
            success: function () { 
                // 用户确认分享后执行的回调函数
            },
            cancel: function () { 
                // 用户取消分享后执行的回调函数
            }
          });
        });
	}

    // 获取分类信息
    $scope.getCategory = function(){
        if($stateParams.id != 0){

            museumFac.product.categories({
                categoryId:$stateParams.id
            },function(data){
                
                if(data.info){
                    $scope.categorys = data.info;
                    $scope.header_chosen = $scope.categorys[0].id;
                }else{
                    $scope.header_chosen = $stateParams.id;
                }

                $scope.getProduct();
                
            },function(data){
                alert(data.data.info);
            });

        }else{

            museumFac.product.products({
                keyword:$rootScope.search_product
            },function(data){
                $scope.products = data.info;

                $scope.checkOpenid();
            },function(data){
                alert(data.data.info);
            });

        }
        
    }
    $scope.getCategory();

    // 获取商品信息
    $scope.getProduct = function(){
        museumFac.product.products({
            categoryId:$scope.header_chosen
        },function(data){
            $scope.products = data.info;
            angular.forEach($scope.categorys, function(item){            
                if(item.id == $scope.header_chosen){
                    $scope.share_name = item.name;
                }
            });
            $scope.checkOpenid();
        },function(data){
            alert(data.data.info);
        });
    }
    
    $scope.add = function(){
        alert('已成功添加至购物车');
        event.stopPropagation();
    }

    $scope.gotoDetail = function(list){
        $location.url('productdetail/' + list.id);
    }

    $scope.gotoCart = function(){
        // $location.url('cart');
        window.location.href = 'http://store.sifangartmuseum.org.cn/Public/museum/www/#/cart';
        event.stopPropagation();
    }

    $scope.gosearch = function(){
        console.log($rootScope.search_product);
        // $location.url('productlist/0');
        museumFac.product.products({
            keyword:$rootScope.search_product
        },function(data){
            $scope.products = data.info;

            $scope.checkOpenid();
        },function(data){
            alert(data.data.info);
        });
    }

    $scope.gotoCenter = function(){
        museumFac.user.usertest(function(data){
            if(data.info == true){
                $location.url('center');
            }else{
                $scope.url = window.location.href;
                $cookies.put('lastPage', $scope.url);
                $location.url('login');
            }
        });
    }

    $scope.backhome = function(){
        $location.url('home');
    }
  
})